﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OMS.Models;
using OMS.OMSService;
using PagedList;

namespace OMS.Controllers
{
    public class MerchController : Controller
    {
        //
        // GET: /Merch/

        public ActionResult Index(string condition, string field, string pattern, int? page)
        {
            ViewBag.Condition = condition;
            ViewBag.Field = field;
            ViewBag.Pattern = pattern;
            List<OrderModelView> orderModelViews = new List<OrderModelView>();
            List<OrderModel> orderModels = new List<OrderModel>();
            UserModel userModel = new UserModel();
            Service1 service = new Service1();
            if (!String.IsNullOrEmpty(pattern))
            {
                orderModels = service.OrderFilter(field, pattern).ToList();
                orderModels = orderModels.Where(m => m.Status.Equals(condition)).ToList();
            }
            else
            {
                orderModels = service.GetAllOrders().ToList();
            }
            foreach (var orderModel in orderModels)
            {
                orderModelViews.Add(new OrderModelView(orderModel));
                if (orderModelViews.Last().Asignee > 0)
                {
                    userModel = service.GetUserById(orderModelViews.Last().Asignee, true);
                    orderModelViews.Last().AsigneeName = userModel.FirstName + ' ' + userModel.LastName;
                }
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(orderModelViews.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult EditOrder(int orderId)
        {
            Service1 service = new Service1();
            OrderModel orderModel = service.GetOrderById(orderId, true);
            OrderModelView orderModelView = new OrderModelView(orderModel);
            return View(orderModelView);
        }

        [HttpPost]
        public ActionResult EditOrder(bool Ordered, bool Delivered, DateTime deliveryDate, int orderId)
        {
            Service1 service = new Service1();
            OrderModel orderModel = service.GetOrderById(orderId, true);
            orderModel.DeliveryDate = deliveryDate;
            orderModel.PrefDeliveryDateSpecified = true;
            if (Ordered)
            {
                orderModel.Status = "ordered";
            }
            if (Delivered)
            {
                orderModel.Status = "delivered";
            }
            bool res = false, resSpec = false;
            service.EditOrder(orderModel, out res, out resSpec);
            if (res)
                return RedirectToAction("Index");
            else
                return RedirectToAction("/Error");
        }

        public ActionResult DeleteOrder(int orderId)
        {
            return View();
        }
    }
}